/*
 * Created by SharpDevelop.
 * User: tentus
 * Date: 2006-10-22
 * Time: 04:02
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Xml;
using System.Xml.XPath;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;

namespace ActionManager
{
	/// <summary>
	/// Description of ActionManagerXml.
	/// </summary>
	/// 
	
	public struct SShortcut
	{
		public Keys key;
		public int modifiers;
		public int nr_grupy;
	}
	
	public struct SSchedule
	{
		public DateTime date;
		public string frequency;
		public int nr_grupy;
	}
	
	
	public class ActionManagerXml
	{
		private XmlDocument document;
		
		public ActionManagerXml()
		{
			document = new XmlDocument();
		}
		
		public string LoadXml(string sciezka)
		{
			if (File.Exists(sciezka)==false)return "";
			
			document.Load(sciezka);
			return document.OuterXml;
		}
		
		public void SaveXml(string _xml, string _path)
		{
			if (_xml=="")return;
			document.LoadXml(_xml);
			document.Save(_path);
		}
		
		private void GetGroup(XPathNavigator _navigator, int _nr_grupy)
		{
			_navigator.MoveToRoot();
			_navigator.MoveToChild("ActionManager", String.Empty);
			_navigator.MoveToChild("ActionGroups", String.Empty);
			_navigator.MoveToChild("Group", String.Empty);
			for (int i=0; i<_nr_grupy; ++i)
				_navigator.MoveToNext();
		}
		
		public string GetGroup(string _xml, int _group)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetGroup(navigator, _group);
			return navigator.OuterXml;
		}
		
		public void GetGroupList(string _xml, List<string> _list)
		{
			if (_xml=="")return;
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetGroup(navigator, 0);
			do
			{				
				if (navigator.MoveToChild("Text", String.Empty)==false)break;				
				_list.Add(navigator.InnerXml);
				navigator.MoveToParent();
			}
			while(navigator.MoveToNext());
		}
		
		public string AddGroup(string _xml, string _xml_group)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			navigator.MoveToChild("ActionManager", String.Empty);
			navigator.MoveToChild("ActionGroups", String.Empty);
			navigator.InnerXml = navigator.InnerXml+_xml_group;
			
			return document.OuterXml;
		}
		
		public string EditGroup(string _xml, string _xml_group, int _group)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetGroup(navigator, _group);
			navigator.OuterXml = _xml_group;
			
			return document.OuterXml;
		}
		
		public string DeleteGroup(string _xml, int _group)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetGroup(navigator, _group);
			navigator.DeleteSelf();
			
			return document.OuterXml;
		}
		
		public string CreateEmptyGroupXml()
		{
			return "<ActionManager><ActionGroups></ActionGroups><Settings></Settings></ActionManager>";
		}
		
		private void GetAction(XPathNavigator _navigator, int _nr_grupy)
		{
			_navigator.MoveToRoot();
			_navigator.MoveToChild("Group", String.Empty);
			_navigator.MoveToChild("ActionList", String.Empty);
			_navigator.MoveToChild("Action", String.Empty);
			for (int i=0; i<_nr_grupy; ++i)
				_navigator.MoveToNext();
		}
		
		public void GetActionList(string _xml_grupa, List<string> _lista)
		{
			if (_xml_grupa=="")return;
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetAction(navigator, 0);			
			do
			{
				if (navigator.MoveToChild("Text", String.Empty)==false)break;				
				_lista.Add(navigator.InnerXml);
				navigator.MoveToParent();
			}
			while(navigator.MoveToNext());						
		}
		
		public string GetAction(string _xml_grupa, int _action)
		{
			if (_xml_grupa=="")return "";
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetAction(navigator, _action);
			return navigator.OuterXml;
		}
		
		public string DeleteAction(string _xml_grupa, int _action)
		{
			if (_xml_grupa=="")return "";
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetAction(navigator, _action);
			navigator.DeleteSelf();
			
			return document.OuterXml;
		}
		
		public string AddAction(string _xml, string _xml_action)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			navigator.MoveToChild("Group", String.Empty);
			navigator.MoveToChild("ActionList", String.Empty);
			navigator.InnerXml = navigator.InnerXml+_xml_action;
			
			return document.OuterXml;
		}
		
		
		public string EditAction(string _xml, string _xml_action, int _action)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetAction(navigator, _action);
			navigator.OuterXml = _xml_action;
			
			return document.OuterXml;
		}
		
		public string MoveUpAction(string _xml, int _action)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetAction(navigator, _action);
			
			string xml_aktu = navigator.InnerXml;
			if (navigator.MoveToPrevious()==false)return _xml;
			
			string xml_previous = navigator.InnerXml;
			navigator.InnerXml = xml_aktu;
			navigator.MoveToNext();
			navigator.InnerXml = xml_previous;
			
			return document.OuterXml;
		}
		
		public string MoveDownAction(string _xml, int _action)
		{
			if (_xml=="")return "";
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetAction(navigator, _action);
			
			string xml_aktu = navigator.InnerXml;
			if (navigator.MoveToNext()==false)return _xml;
			
			string xml_next = navigator.InnerXml;
			navigator.InnerXml = xml_aktu;
			navigator.MoveToPrevious();
			navigator.InnerXml = xml_next;
			
			return document.OuterXml;
		}
		
		public string GetGroupName(string _xml_grupa)
		{			
			if (_xml_grupa=="")return "";
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();
			navigator.MoveToRoot();
			navigator.MoveToChild("Group", String.Empty);
			//navigator.MoveToChild("Name", String.Empty);
			navigator.MoveToChild("Text", String.Empty);
			
			return navigator.InnerXml;
		}
		
		private void GetScheduleFromXml(ref XPathNavigator navigator, ref string _date, ref string _time, ref string _frequency)
		{
			if (navigator.MoveToChild("Date", String.Empty))
			{
				_date = navigator.InnerXml;
				navigator.MoveToParent();
			}
			
			if (navigator.MoveToChild("Time", String.Empty))
			{
				_time = navigator.InnerXml;
				navigator.MoveToParent();
			}
			
			if (navigator.MoveToChild("Frequency", String.Empty))
			{
				if (navigator.MoveToChild("Once", String.Empty))
					_frequency = "Once";
				
				if (navigator.MoveToChild("Daily", String.Empty))
					_frequency = "Daily";
				
				if (navigator.MoveToChild("Weekly", String.Empty))
					_frequency = "Weekly";
					
				if (navigator.MoveToChild("Workdays", String.Empty))
					_frequency = "Workdays";
				
				if (navigator.MoveToChild("Monthly", String.Empty))
					_frequency = "Monthly";
				
				if (navigator.MoveToChild("Yearly", String.Empty))
					_frequency = "Yearly";
			}
		}
		
		public void GetActionType(string _xml_grupa, ref string _shortcut, ref string _date, ref string _time, ref string _frequency, ref string _type)
		{
			if (_xml_grupa=="")return;
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();
			navigator.MoveToRoot();
			navigator.MoveToChild("Group", String.Empty);
			navigator.MoveToChild("ExecuteType", String.Empty);
			if(navigator.MoveToChild("Shortcut", String.Empty))
			{
				if (navigator.MoveToChild("Text", String.Empty))
					_shortcut = navigator.InnerXml;
			}
			
			navigator.MoveToRoot();
			navigator.MoveToChild("Group", String.Empty);
			navigator.MoveToChild("ExecuteType", String.Empty);
			if(navigator.MoveToChild("Schedule", String.Empty))
			{
				GetScheduleFromXml(ref navigator, ref _date, ref _time, ref _frequency);
			}
			
			navigator.MoveToRoot();
			navigator.MoveToChild("Group", String.Empty);
			navigator.MoveToChild("ExecuteType", String.Empty);
			if(navigator.MoveToChild("Type", String.Empty))
			  _type = navigator.InnerXml;								
		}
		
		public string CreateDefaultGroup(string _group_name, string _shortcut, string _frequency, string _type)
		{
			return "<Group>"+//<Name>"+_group_name+"</Name>"+
				   "<Text>"+_group_name+"</Text>"+
				   "<ExecuteType>"+
				   "<ShortCut><Text>"+_shortcut+"</Text></ShortCut>"+
				   "<Schedule><Frequency><"+_frequency+">1</"+_frequency+"></Frequency></Schedule>"+
				   "<Type>"+_type+"</Type>"+
				   "</ExecuteType>"+
				   "<ActionList></ActionList></Group>";
		}
		
		public string SetGroupName(string _xml_grupa, string _name)
		{
			if (_xml_grupa=="")return "";
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();				
			navigator.MoveToRoot();
			navigator.MoveToChild("Group", String.Empty);
			//navigator.MoveToChild("Name", String.Empty);
			//navigator.InnerXml = _name;
			//navigator.MoveToParent();
			navigator.MoveToChild("Text", String.Empty);
			navigator.InnerXml = _name;
			
			return document.OuterXml;
		}
		
		private string SetShortcut(string _shortcut)
		{
			string tekst = _shortcut;			
			string alt   = "0";
			string ctrl  = "0";
			string shift = "0";
			string kod   = "0";
				
			if (tekst.IndexOf("Alt+")>=0)
			{
				tekst = tekst.Remove(tekst.IndexOf("Alt+"), 4);
				alt = "1";
			}
			    
			if (tekst.IndexOf("Ctrl+")>=0)
			{
			    tekst = tekst.Remove(tekst.IndexOf("Ctrl+"), 5);
				ctrl = "1";
			}
			    
			if (tekst.IndexOf("Shift+")>=0)
			{
			    tekst = tekst.Remove(tekst.IndexOf("Shift+"), 6);
				shift = "1";
			}
			
			kod = tekst;
			
			return "<Shortcut>"+
				   "<Text>"+_shortcut+"</Text>"+
				   "<Alt>"+alt+"</Alt>"+
				   "<Ctrl>"+ctrl+"</Ctrl>"+
				   "<Shift>"+shift+"</Shift>"+
				   "<Keycode>"+kod+"</Keycode>"+
				   "</Shortcut>";
		}
		
		private string SetFrequency(string _frequency)
		{
			return "<"+_frequency+">1</"+_frequency+">";
		}
				
		private string SetType(string _type)
		{
			return "<Type>"+_type+"</Type>";	
		}
		
		public string SetActionType(string _xml_grupa, string _shortcut, string _date, string _time, string _frequency, string _type)
		{
			if (_xml_grupa=="")return "";
			document.LoadXml(_xml_grupa);
			
			XPathNavigator navigator = document.CreateNavigator();										
			
			string xml_shortcut = SetShortcut(_shortcut);
			
			string xml_freq = SetFrequency(_frequency);
			
			string xml_type = SetType(_type);
			
			string xml_schedule = "<Schedule>"+
				                  "<Date>"+_date+"</Date>"+
				                  "<Time>"+_time+"</Time>"+
				                  "<Frequency>"+xml_freq+"</Frequency>"+
				                  "</Schedule>";
			
			navigator.MoveToRoot();
			navigator.MoveToChild("Group", String.Empty);
			navigator.MoveToChild("ExecuteType", String.Empty);
			navigator.InnerXml = xml_shortcut+xml_schedule+xml_type;
									
			return document.OuterXml;
		}
		
		
		private int GetKeyModifiers(string _ctrl, string _shift, string _alt)
		{
			int zwroc = 0;
			
			if (_ctrl=="1")
				zwroc +=2;
			
			if (_shift=="1")
				zwroc +=4;
			
			if (_alt=="1")
				zwroc +=1;
			
			return zwroc;
		}
		
		private SShortcut GetShortcut(XmlNode _node)
		{
			SShortcut zwroc = new SShortcut();
			string alt, ctrl, shift, kod;
			
			XPathNavigator navigator = _node.CreateNavigator();
			navigator.MoveToChild("ExecuteType", String.Empty);
			navigator.MoveToChild("Shortcut", String.Empty);
			navigator.MoveToChild("Alt", String.Empty);
			alt = navigator.InnerXml;
			
			navigator.MoveToParent();
			navigator.MoveToChild("Ctrl", String.Empty);
			ctrl = navigator.InnerXml;
			
			navigator.MoveToParent();
			navigator.MoveToChild("Shift", String.Empty);
			shift = navigator.InnerXml;
			
			navigator.MoveToParent();
			navigator.MoveToChild("Keycode", String.Empty);
			kod = navigator.InnerXml;
			
			if (kod!="")
			  zwroc.key = (Keys)TypeDescriptor.GetConverter(typeof(Keys)).ConvertFromString(kod);
			else
			  zwroc.key = Keys.None;
						
			zwroc.modifiers = GetKeyModifiers(ctrl, shift, alt);
			
			return zwroc;
		}
		
		private bool IsShortcut(XmlNode _node)
		{
			XPathNavigator navigator = _node.CreateNavigator();			
			navigator.MoveToChild("ExecuteType", String.Empty);
			
			string x = navigator.InnerXml;
			if (navigator.MoveToChild("Type", String.Empty)==false)return false;
			
			if (navigator.InnerXml=="Shortcut")
				return true;
			
			return false;
		}
		
		public void GetShortcutList(string _xml, List<SShortcut> _list)
		{
			if (_xml=="")return;
			document.LoadXml(_xml);
			
			XmlNodeList nodeList = document.DocumentElement.SelectNodes("//Group");
			
			int indx = 0;
			foreach (XmlNode node in nodeList)
            { 
				if (IsShortcut(node))
				{
					SShortcut struct_shortcut = GetShortcut(node);
					struct_shortcut.nr_grupy  = indx;
					_list.Add(struct_shortcut);
				}
				indx++;
            }						
		}
		
		private SSchedule GetSchedule(XmlNode _node)
		{
			SSchedule zwroc = new SSchedule();
			string date = "", time = "", frequency = "";
			
			XPathNavigator navigator = _node.CreateNavigator();
			navigator.MoveToChild("ExecuteType", String.Empty);
			navigator.MoveToChild("Schedule", String.Empty);						
			
			GetScheduleFromXml(ref navigator, ref date, ref time, ref frequency);
			
			DateTime d = (DateTime)TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString(date);
			DateTime t = (DateTime)TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString(time);						
			
			zwroc.date = new DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, 0);
			zwroc.frequency = frequency;
						
			return zwroc;
		}
		
		private bool IsSchedule(XmlNode _node)
		{
			XPathNavigator navigator = _node.CreateNavigator();
			navigator.MoveToChild("ExecuteType", String.Empty);
			if (navigator.MoveToChild("Type", String.Empty)==false)return false;
			
			if (navigator.InnerXml=="Schedule")
				return true;
			
			return false;
		}
		
		
		public void GetScheduleList(string _xml, List<SSchedule> _list)
		{
			if (_xml=="")return;
			document.LoadXml(_xml);
			
			XmlNodeList nodeList = document.DocumentElement.SelectNodes("//Group");
			
			int indx = 0;
			foreach (XmlNode node in nodeList)
            { 
				if (IsSchedule(node))
				{
					SSchedule struct_schedule = GetSchedule(node);
					struct_schedule.nr_grupy  = indx;
					_list.Add(struct_schedule);
				}
				indx++;
            }
		}
		
		public void ExecuteGroup(string _xml, int _indx)
		{
			if (_xml=="")return;
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			GetGroup(navigator, _indx);
			navigator.MoveToChild("ActionList", String.Empty);	
			if (navigator.MoveToChild("Action", String.Empty)==false)return;
			
			do
			{
				navigator.MoveToChild("Filename", String.Empty);
				string plik = navigator.InnerXml;
				
				navigator.MoveToParent();
				navigator.MoveToChild("ActionSettings", String.Empty);
				string settings = navigator.InnerXml;
				
				ExecuteAction(plik, settings);
				
				navigator.MoveToParent();
			}
			while(navigator.MoveToNext());
			
		}
		
		public void ExecuteAction(string _file_name, string _settings)
		{
			ActionDll actionDll = new ActionDll();
			actionDll.ExecuteAction(_file_name, _settings);			
		}
		
		public string getActionXml(string _filename, string _text, string _settings)
		{
			return "<Action><Filename>"+_filename+"</Filename><Text>"+_text+"</Text><ActionSettings>"+_settings+"</ActionSettings></Action>";
		}
		
		public string EditActionDll(string _xml)
		{
			if (_xml=="")return _xml;
			document.LoadXml(_xml);
			
			XPathNavigator navigator = document.CreateNavigator();
			if (navigator.MoveToChild("Action", String.Empty)==false)return _xml;
			
			navigator.MoveToChild("Filename", String.Empty);
			string plik     = navigator.InnerXml;
			
			navigator.MoveToParent();
			navigator.MoveToChild("Text", String.Empty);
			string text = navigator.InnerXml;
			
			navigator.MoveToParent();
			navigator.MoveToChild("ActionSettings", String.Empty);
			string settings = navigator.InnerXml;
			
			ActionDll actionDll = new ActionDll();
			actionDll.GetActionSettings(plik, ref settings);
			
			return getActionXml(plik, text, settings);
		}				
	}
}
